import Utils.JdbcUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 门户登录->数字化平台->点"河南"跳转到数智豫电平台（个人刷数据使用）
 *
 * @author admin
 */
public class Brush_Mh_Szh_Szhn {
    public static void main(String[] args) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        //查询数据库中的用户信息
        PreparedStatement pre = conn.prepareStatement("SELECT id,user_name,pass_word,status " + "FROM `inv_user50` " + "where status = 0 ");
        //返回结果
        ResultSet rs = pre.executeQuery();

        int i = 0;
        int j = 0;
        Date startDate = new Date();
        while (rs.next()) {
            //指定配置
            System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
            //隐藏浏览器
            ChromeOptions chromeOptions = new ChromeOptions();
            //后台模式
            chromeOptions.addArguments("--headless");
//            chromeOptions.addArguments("--start-maximized");
            //声明浏览器
            ChromeDriver driver = new ChromeDriver(chromeOptions);
            try {
                //门户登录
                driver.get("http://nsystem.ha.sgcc.com.cn/");

                driver.findElement(By.id("username")).sendKeys(rs.getString(2));
                driver.findElement(By.id("password")).sendKeys(rs.getString(3));
                driver.findElement(By.id("submit_login")).click();

                driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
                driver.getPageSource();

//                String windowHandleMh = driver.getWindowHandle();
//                System.out.println("门户：" + windowHandleMh);

                //如果用户没登陆过会是其他页面，制造异常
                if (!Brush_Mh_Szh_Szhn_2.hasElement(driver, By.cssSelector("li:nth-child(9) > span img"))) {
                    System.out.println(1 / 0);
                }

                //数智豫电图标
//                driver.findElement(By.cssSelector("li:nth-child(9) > span img")).click();
                driver.findElement(By.xpath("//img[@title='数智豫电']")).click();

//                String windowHandleSzh = driver.getWindowHandle();
//                System.out.println("数字：" + windowHandleSzh);

                Set<String> windowHandleSzhList = driver.getWindowHandles();
                for (String handle : windowHandleSzhList) {
                    if (driver.getWindowHandle().equals(handle)) {
//                        System.out.println("跳过：" + handle);
                        continue;
                    }
                    driver.switchTo().window(handle);
                }

                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.getPageSource();

                //各省
                driver.findElement(By.linkText("各省")).click();
                //河南
                driver.findElement(By.cssSelector(".content-box:nth-child(14) > i")).click();

                Thread.sleep(5000);

                String sqlUpdate = "UPDATE inv_user50 SET status = 1 where id = ?";
                PreparedStatement pre3 = conn.prepareStatement(sqlUpdate);
                pre3.setString(1, rs.getString(1));
                pre3.executeUpdate();
                i++;
                long time = (new Date().getTime() - startDate.getTime()) / 1000 / 60;
                System.out.println("执行" + time + "分钟，成功" + i + "个！");
                driver.quit();  // 关闭浏览器
            } catch (Exception e) {
                updateStatusError(conn, rs);
                j++;
                long time = (new Date().getTime() - startDate.getTime()) / 1000 / 60;
                System.out.println("执行" + time + "分钟，失败" + j + "个！");
                driver.quit();  // 关闭浏览器
            }
        }
    }

    public static void updateStatusError(Connection conn, ResultSet rs) throws SQLException {
        //catch模块暂
        String sqlUpdate = "UPDATE inv_user50 SET status = 2 where id = ?";
        PreparedStatement pre3 = conn.prepareStatement(sqlUpdate);
        pre3.setString(1, rs.getString(1));
        pre3.executeUpdate();
    }

}
